from zauxdllPython import ZAUXDLL
import time


class Controller:
    def __init__(self, ip_addr):
        self.zaux = ZAUXDLL()
        self.ip_addr = ip_addr

    def connect(self):
        ret = self.zaux.ZAux_OpenEth(self.ip_addr)
        if ret != 0:
            print("connect controller failed!")
            self.zaux.handle = None
            input()
            exit(-1)
        print("控制器连接成功！")

    def close(self):
        self.zaux.ZAux_Close()  # 关闭连接
        print("connection closed!")
        self.zaux.handle = None

    def set_axis(self, axis, units=100, speed=200, accel=2000, decel=2000):
        self.zaux.ZAux_Direct_SetAtype(axis, 1)
        self.zaux.ZAux_Direct_SetUnits(axis, units)
        self.zaux.ZAux_Direct_SetSpeed(axis, speed)
        self.zaux.ZAux_Direct_SetAccel(axis, accel)
        self.zaux.ZAux_Direct_SetDecel(axis, decel)

    def single_move(self, axis, distance):
        self.zaux.ZAux_Direct_Single_Move(axis, distance)
        # self._wait_axis_move(axis)

    def _wait_axis_move(self, axis):
        while True:  # 等待轴0运动完成
            time.sleep(0.1)
            idle = self.zaux.ZAux_Direct_GetParam("IDLE", axis)[0]
            print(idle)
            if idle < 0:
                print(idle)
                break

    def set_axis_pos(self, axis, pos):
        self.zaux.ZAux_Direct_SetDpos(axis, pos)
        self.zaux.ZAux_Direct_SetMpos(axis, pos)


if __name__ == "__main__":
    ip_addr = "192.168.100.110"  # 控制器IP地址
    controller = Controller(ip_addr)
    controller.connect()
    controller.set_axis(0)
    controller.single_move(0, -100)
    controller.close()